package com.lujianfei.powerinput.widget.input;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lujianfei.powerinput.R;
import com.lujianfei.powerinput.widget.input.base.IWidgetInput;

public class TextInput extends LinearLayout implements IWidgetInput {

    private TextView txt_title;
    private EditText edit_input;
    private String key;
    private ImageView img_delete;
    private String className;
    private OnClickListener onWidgetDeleteListener;
    public TextInput(Context context) {
        super(context);
        initView(context);
    }

    public TextInput(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TextInput(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_text_input,this,true);
        txt_title = findViewById(R.id.txt_title);
        edit_input = findViewById(R.id.edit_input);
        img_delete = findViewById(R.id.img_delete);
        img_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null!=onWidgetDeleteListener) {
                    onWidgetDeleteListener.onClick(TextInput.this);
                }
            }
        });
    }

    @Override
    public void setWidgetClassName(String className) {
        this.className = className;
    }

    @Override
    public String getWidgetClassName() {
        return className;
    }

    @Override
    public void setWidgetLabel(String label) {
        txt_title.setText(label);
    }

    @Override
    public void setWidgetKey(String key) {
        this.key = key;
    }

    @Override
    public String getWidgetLabel() {
        return txt_title.getText().toString();
    }

    @Override
    public String getWidgetKey() {
        return key;
    }

    @Override
    public void setWidgetValue(String value) {
        edit_input.setText(value);
    }

    @Override
    public String getWidgetValue() {
        return edit_input.getText().toString();
    }

    @Override
    public void showDelete() {
        img_delete.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideDelete() {
        img_delete.setVisibility(View.GONE);
    }

    @Override
    public void setOnWidgetDeleteListener(OnClickListener onWidgetDeleteListener) {
        if (null!=onWidgetDeleteListener){
            this.onWidgetDeleteListener = onWidgetDeleteListener;
        }
    }


    @Override
    public View getWidgetView() {
        return this;
    }

    @Override
    public String getWidgetName() {
        return "文本输入";
    }

}
